package situ.com.musicplayer;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MusicPlayerActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "MusicPlayerActivity";

    private static SeekBar mSeekBar;
    private static TextView mTvProgress;
    private static TextView mTvTotal;
    private ImageView mIvPlay;
    private ImageView mIvNext;
    private ImageView mIvPrevious;

    private String mPath;
    private ArrayList<String> mList;
    private int mCurrentIndex;

    private MusicService.MusicControll mMusicControll;

    public static Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            Bundle bundle = msg.getData();
            // 总时长
            int duration = bundle.getInt("duration");
            // 当前播放时长
            int currentPosition = bundle.getInt("currentPosition");
            mSeekBar.setMax(duration);
            mSeekBar.setProgress(currentPosition);

            // 显示总时长
            int minute = duration / 1000 / 60;
            int second = duration / 1000 % 60;
            mTvTotal.setText(minute + ":" + second);

            // 显示播放时长
            int currentMinute = currentPosition / 1000 / 60;
            int currentSecond = currentPosition / 1000 % 60;
            mTvProgress.setText(currentMinute + ":" + currentSecond);
        };
    };


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_play:
                if (mMusicControll.isPlaying()) {// 如果当前是在播放状态，就暂停
                    mMusicControll.pause();
                    mIvPlay.setImageDrawable(getResources().getDrawable(R.drawable.playbar_pause));
                } else {// 如果是暂停状态，就改为播放
                    mMusicControll.play();
                    mIvPlay.setImageDrawable(getResources().getDrawable(R.drawable.playbar_play));
                }
                break;
            case R.id.iv_next:
                Log.d(TAG, "next");
                if (mCurrentIndex == mList.size() - 1){
                    Toast.makeText(this, "这已经是最后一首歌了", Toast.LENGTH_LONG).show();
                    return;
                }
                mCurrentIndex++;
                mMusicControll.init(mList.get(mCurrentIndex));
                break;
            case R.id.iv_previous:
                Log.d(TAG, "previous");
                if (mCurrentIndex == 0){
                    Toast.makeText(this, "这已经是第一首歌了", Toast.LENGTH_LONG).show();
                    return;
                }
                mCurrentIndex--;
                mMusicControll.init(mList.get(mCurrentIndex));
                break;
        }
    }

    //  // ServiceConnection:用来绑定客户端和服务器，就是用来绑定Service
    private class MyConnection implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 綁定Service
            mMusicControll = (MusicService.MusicControll) service;
            mMusicControll.init(mPath);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_player);
        mIvPlay = (ImageView) findViewById(R.id.iv_play);
        mIvNext = (ImageView) findViewById(R.id.iv_next);
        mIvPrevious = (ImageView) findViewById(R.id.iv_previous);
        mSeekBar = (SeekBar) findViewById(R.id.seekBar);
        mTvProgress = (TextView) findViewById(R.id.tv_progress);
        mTvTotal = (TextView) findViewById(R.id.tv_total);

        mIvPlay.setOnClickListener(this);
        mIvNext.setOnClickListener(this);
        mIvPrevious.setOnClickListener(this);

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            // 停止滑动时候处理
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 得到当前拖动停止的位置
                int progress = seekBar.getProgress();
                // 调用服务的seekTo方法改变音乐的进度
                mMusicControll.seekTo(progress);
            }

            // 开始滑动时候处理
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            // 滑动条变化时候吹
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                // TODO Auto-generated method stub

            }
        });

        Intent intent = getIntent();
        mPath = intent.getStringExtra("path");
        Log.d(TAG, "mPath: " + mPath);
        mList = (ArrayList<String>) intent.getSerializableExtra("mp3List");
        // 获得当前播放文件在List中的索引
        mCurrentIndex = mList.indexOf(mPath);

        // 绑定Service
        intent = new Intent(this, MusicService.class);
        bindService(intent, new MyConnection(), BIND_AUTO_CREATE);
    }
}
